<?php

declare(strict_types=1);

namespace App\Controller\Home\Drp;


use App\Model\Coupon;
use App\Model\CouponRecord;
use App\Model\Device;
use App\Model\Station;
use App\Model\User;
use App\Model\White;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use App\Model\Distributors;

class WhiteController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Device $device;


    #[Inject]
    protected Coupon $coupon;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**白名单审核列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function checkList(RequestInterface $request):ResponseInterface{
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $is_check = $request->input('is_check');
        $where['distributor_id'] = $info['id'];
        if($is_check != ''){
            $where['is_check'] = $is_check;
        }
        $list = White::where($where)
            ->get();
        foreach($list as &$v){
            $v['sn'] = Device::where('id',$v['device_id'])->value('sn');
            $v['user'] = User::where('id',$v['user_id'])->first(['nickname','avatar']);
        }
        return $this->success($list);
    }

    /**白名单添加
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function checkWhite(RequestInterface $request):ResponseInterface{
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $id = $request->input('id');
        $is_check  = $request->input('is_check');
        if(!$id){
            return $this->error(code: '1001', msg: '参数错误');
        }
        try{
            $whiteModel = White::where('id',$id)->first();
            $whiteModel->is_check = $is_check;
            $whiteModel->save();
            $keys = 'WHITE_DEVICE_'.$whiteModel->device_id;
            if($is_check){
                $this->redis->sAdd($keys,$whiteModel->user_id);
            }
            return $this->success();
        }catch (\Exception $e){
            return $this->error(1001,$e->getMessage());
        }
    }

    /**设备白名单列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function whiteList(RequestInterface $request):ResponseInterface{
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        if($info['level'] == 1){
            $devices = Device::where('distributor_id',$info['id'])
                ->where('child_distributor_id',0)
                ->get(['sn','id']);
        }else{
            $devices = Device::where('child_distributor_id',$info['id'])
                ->get(['sn','id']);
        }
        foreach($devices as $k => $v){
            $keys = 'WHITE_DEVICE_'.$v['id'];
            $users = redis()->sMembers($keys);
            $devices[$k]['white_users'] = User::whereIn('id',$users)->get(['id','nickname','avatar']);
        }
        return $this->success($devices);
    }

    /**为设备移除白名单
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function delWhite(RequestInterface $request):ResponseInterface{
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $deviceId = $request->input('device_id');
        $userId = $request->input('user_id');
        $keys = 'WHITE_DEVICE_'.$deviceId;
        redis()->sRem($keys,$userId);
        White::where('device_id',$deviceId)
            ->where('user_id',$userId)
            ->delete();
        return $this->success();
    }



}
